﻿#ifndef RTCCLIENT_H
#define RTCCLIENT_H

#include <QObject>
#include <QLocalSocket>
#include <QByteArray>
#include <QDataStream>
#include <QString>
#include <unordered_map>
#include "RTCDefine.h"
#include <engine_interface.h>
#include "rtcaudioframeobserver.h"

namespace RTC3 = ding::rtc;

class AliRtcEventHandler;

// 返回 bool 值的宏（防止越界）
#define RETURN_BOOL(request) \
    ((request.parameters.size() > 0) ? (request.parameters.at(0).toBool()) : (qWarning() << "Index 0 out of range for parameters, returning false.", false))

// 返回 int 值的宏（防止越界）
#define RETURN_INT(request) \
    ((request.parameters.size() > 0) ? (request.parameters.at(0).toInt()) : (qWarning() << "Index 0 out of range for parameters, returning 0.", 0))

// 返回 QString 值的宏（防止越界）
#define RETURN_STRING(request) \
    ((request.parameters.size() > 0) ? (request.parameters.at(0).toString()) : (qWarning() << "Index 0 out of range for parameters, returning empty string.", QString("")))

// 返回 bool 值的宏，带索引参数（防止越界）
#define RETURN_BOOL_INDEX(request, index) \
    (((index) >= 0 && (index) < request.parameters.size()) ? (request.parameters.at(index).toBool()) : (qWarning() << "Index" << index << "out of range for parameters, returning false.", false))

// 返回 int 值的宏，带索引参数（防止越界）
#define RETURN_INT_INDEX(request, index) \
    (((index) >= 0 && (index) < request.parameters.size()) ? (request.parameters.at(index).toInt()) : (qWarning() << "Index" << index << "out of range for parameters, returning 0.", 0))

// 返回 QString 值的宏，带索引参数（防止越界）
#define RETURN_STRING_INDEX(request, index) \
    (((index) >= 0 && (index) < request.parameters.size()) ? (request.parameters.at(index).toString()) : (qWarning() << "Index" << index << "out of range for parameters, returning empty string.", QString("")))


class RTCClient : public QObject {
    Q_OBJECT

public:
    explicit RTCClient(QObject *parent = nullptr);
    ~RTCClient();
    
    // 连接到Teacher的RTCService
    void connectToTeacher();
    
    // 检查连接状态
    bool isConnected() const;
    
public slots:
    void processCallback(RTCEngineCallback);
    
    // 设置MMService引用，用于转发回调
    void setMMService(QObject* mmService) { m_mmService = mmService; }
    
    // 音频帧观察者管理
    void registerAudioFrameObserver();  // 注册音频帧观察者回调
    void enableAudioFrameObserver(bool enabled);  // 启用/禁用音频帧观察者
    void setAudioFrameObserverConfig(const ding::rtc::RtcEngineAudioFrameObserverConfig &config);
    ding::rtc::RTCAudioFrameObserver* getAudioFrameObserver() { return m_audioFrameObserver; }
    
private slots:
    void onRequestConnected();
    void onRequestReadyRead();
    void onRequestDisconnected();
    
    void onCallbackConnected();
    void onCallbackReadyRead();
    void onCallbackDisconnected();

signals:
    void exit();
    void rtcFullyConnected(); // 新增信号：RTC完全连接时发出

private:
    QByteArray requestBuffer;
    QByteArray callbackBuffer;
    void processRequestBuffer();
    void processCallbackBuffer();
    
    QLocalSocket *requestSocket = nullptr;   // 连接到 RTCService
    QLocalSocket *callbackSocket = nullptr;  // 连接到 RTCService_Callback

    QByteArray serializeResponse(const RTCEngineResponse &response,int flag = 1) const;
    RTCEngineRequest deserializeRequest(const QByteArray &requestData) const;
    void processRequest(QLocalSocket *socket, const RTCEngineRequest &request,RTCEngineResponse&);
    QByteArray serializeCallback(const RTCEngineCallback &callback, int flag = 2) const;

    // MMService引用，用于转发回调
    QObject* m_mmService = nullptr;

    RTC3::RtcEngine* engine = nullptr;

    RTC3::RtcEngineAudioDeviceManager* audioDeviceManager = nullptr;
    RTC3::RtcEngineVideoDeviceManager* videoDeviceManager = nullptr;
    RTC3::RtcEngineAudioMixingManager* audioMixingManager = nullptr;

    AliRtcEventHandler *eventhandler = nullptr;

    ding::rtc::RTCAudioFrameObserver* m_audioFrameObserver = nullptr;
};

#endif // RTCCLIENT_H 
